//
//  BJDynamicGoodArticleVC.m
//  jinwang
//
//  Created by jzb on 2018/3/16.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "BJDynamicGoodArticleVC.h"
#import "BJDynamicGoodArticleCell.h"
#import "BJDynamicSnapshotModel.h"
#import "JWAlertChoseButsView.h"
#import "ReleaseBoJianVC.h"
#import "JWBoJianDetailsVC.h"

@interface BJDynamicGoodArticleVC ()<LSXPopMenuDelegate>
{
    NSString *post_order;
    NSString *user_sex;
    NSString *user_active;
    NSString *region_id;
    NSString *user_age;
    NSString *user_hobby;
    NSString *user_occupation;
    NSString *user_type;
    NSString *user_rank;
    NSString *is_search;
    NSString *post_cate;
    TYAlertController *_alertController;
    
}
@property (nonatomic,strong) BJDynamicGoodArticleCell *cell;

@property (nonatomic,strong) BJDynamicSnapshotModel *Listmodel,*Exitmodel;
@property (nonatomic,assign) int page;

@property (nonatomic,strong) NSMutableArray *ListmodelArr;

@end

@implementation BJDynamicGoodArticleVC

- (void)viewDidLoad {
    [super viewDidLoad];

    post_order = @"0";
    user_sex = @"2";
    user_active = @"4";
    region_id = @"0";
    user_age = @"1,100";
    user_hobby = @"";
    user_occupation = @"";
    user_type = @"0";
    user_rank = @"0";
    is_search = @"0";
    post_cate = @"0";
    _page = 1;
    [self setUpTableView];
    [self net_makeUrl_postBJList];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JWBJSiftNotificationAction:) name:@"JWBJSiftNotification" object:nil];
    if (_VCType == 0) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JWBJDynamicNotificationAction:) name:@"JWBJDynamicNotification" object:nil];
    }
    if (_VCType == 1) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JWBJHotListNotificationAction:) name:@"JWBJHotListNotification" object:nil];
    }
}
- (void)JWBJSiftNotificationAction:(NSNotification *)notification{
    
    NSLog(@"%@",notification.userInfo[@"user_rank"]);
    
    NSLog(@"---接收到通知---");
    user_sex = notification.userInfo[@"user_sex"];
    user_active = notification.userInfo[@"user_active"];
    region_id = notification.userInfo[@"region_id"];
    user_age = notification.userInfo[@"user_age"];
    user_hobby = notification.userInfo[@"user_hobby"];
    user_occupation = notification.userInfo[@"user_occupation"];
    user_type = notification.userInfo[@"user_type"];
    user_rank = notification.userInfo[@"user_rank"];
//    is_search = @"1";
    
    [self net_makeUrl_postBJList];
    
}
- (void)JWBJDynamicNotificationAction:(NSNotification *)notification{
    
    post_cate = notification.userInfo[@"post_cate"];
//    is_search = @"1";
    [self net_makeUrl_postBJList];
    
}
- (void)JWBJHotListNotificationAction:(NSNotification *)notification{
    post_order = notification.userInfo[@"post_order"];
//    is_search = @"1";
    [self net_makeUrl_postBJList];
}

//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    
    [self registerCellWithCellClassArray:@[NSStringFromClass([BJDynamicGoodArticleCell class])]];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self net_makeUrl_postBJList];
    }];
    //上啦加载更多
    self.tableView.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        _page++;
        [self net_makeUrl_postBJList];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _ListmodelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[BJDynamicGoodArticleCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    [self.cell.upDownBtn addTarget:self action:@selector(cellBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    self.cell.upDownBtn.tag = 1000+indexPath.row;
    BJDynamicSnapshotModel *model = self.ListmodelArr[indexPath.row];
    self.cell.model = model;
    
    for (UIView *view in self.cell.imgsBGView.subviews) {
        
        [view removeFromSuperview];
    }
    if (model.imgs.count > 0) {
        
        self.cell.imgsBGView.imageUrls = model.imgs;
    }
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BJDynamicSnapshotModel *model = self.ListmodelArr[indexPath.row];
    
    if (model.imgs.count > 0) {
        
        return 150-17 + [self cellContentViewWith:self.cell.imgsBGView.width withModel:model];
    }else{
        
       return 150;
    }
}
- (CGFloat)cellContentViewWith:(CGFloat)width withModel:(BJDynamicSnapshotModel *)model
{
    CGFloat Height = (width - 20)/3 + 5;
    CGFloat CellHeight = 0;
    
    CGSize size = CGSizeMake(kScreenWidth - 30, MAXFLOAT);//设置高度宽度的最大限度
    CGRect rect = [model.post_title boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    
    if (model.imgs.count % 3 > 0) {
        
        CellHeight = Height*(model.imgs.count/3+1);
        
    }else{
        
        CellHeight = Height*(model.imgs.count/3);
    }
    
    return CellHeight + rect.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWBoJianDetailsVC *vc = [JWBoJianDetailsVC new];
    BJDynamicSnapshotModel *model = self.ListmodelArr[indexPath.row];
    vc.post_id = [NSString stringWithFormat:@"%@",model.post_id];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)cellBtnOnclick:(UIButton *)sender{
    
    _Exitmodel = self.ListmodelArr[sender.tag - 1000];
    
    if ([_Exitmodel.user_id integerValue] == [k_user_uid integerValue]) {
        
        [LSXPopMenu showRelyOnView:sender titles:@[@"编辑",@"删除"]  icons:@[@"jw_find_bojian_editIcon",@"jw_find_bojian_deleteIocn"] menuWidth:100 isShowTriangle:YES delegate:self];
    }else{
        
        [LSXPopMenu showRelyOnView:sender titles:@[@"举报",@"拉黑",@"只给一窗",@"关注",@"收藏",@"推荐",@"悄悄关注"]  icons:@[@"jw_find_bojian_reportIcon",@"jw_find_bojian_blackIcon",@"jw_find_bojian_window",@"jw_find_bojian_WatchingIcon",@"jw_find_bojian_collectionIcon",@"jw_find_bojian_recommendIcon",@"jw_find_bojian_quietlyWatchingIcon"] menuWidth:120 isShowTriangle:YES delegate:self];
    }
    
}
-(void)LSXPopupMenuDidSelectedAtIndex:(NSInteger)index LSXPopupMenu:(LSXPopMenu *)LSXPopupMenu{
    
    if ([_Exitmodel.user_id integerValue] == [k_user_uid integerValue]) {
        
        if (index == 0) {
            
            ReleaseBoJianVC *vc = [ReleaseBoJianVC new];
            vc.model = _Exitmodel;
            vc.ExitType = @"佳文";
            [self.navigationController pushViewController:vc animated:YES];
        }else if (index == 1){
            UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                           message:@"是否删除本条博见？"
                                                                    preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault
                                                                  handler:^(UIAlertAction * action) {}];
            UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action) {
                                                                     [self.ListmodelArr removeObject:_Exitmodel];
                                                                     [self makeUrl_post_delPost];
                                                                 }];
            
            [alert addAction:defaultAction];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];
            
        }
    }else{
        if (index == 0) {
            //举报
            JWAlertChoseButsView *view = [[JWAlertChoseButsView alloc]initWithFrame:CGRM(0, 0, 750-160, 400)];
            
            view.StrOrArr = @"jubao";
            view.dataList = @[@"广告类",@"诈骗类",@"骚扰类",@"违法类"];
            
            view.layer.cornerRadius = 8.0;
            view.layer.masksToBounds = YES;
            
            view.ProfessionaDateBlcok = ^(NSString *text,NSMutableArray *arr){
                
                if (text.length > 0) {
                    NSString *port_type = @"";
                    if ([text isEqualToString:@"广告类"]) {
                        port_type = @"0";
                    }else if ([text isEqualToString:@"诈骗类"]){
                        port_type = @"1";
                    }else if ([text isEqualToString:@"骚扰类"]){
                        port_type = @"2";
                    }else if ([text isEqualToString:@"违法类"]){
                        port_type = @"3";
                    }
                    [self makeUrl_post_addReportrePort_type:port_type];
                }
            };
            
            _alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
            
            _alertController.backgoundTapDismissEnable = YES;
            [self presentViewController:_alertController animated:YES completion:nil];
        }else if (index == 1){
            //拉黑
            [self makeUrl_post_addBlack];
        }else if (index == 2){
            //只给一窗
            [self makeUrl_post_addOne];
        }
        else if (index == 3){
            //关注
            [self makeUrl_post_addFollowFollow_type:@"0"];
        }
        else if (index == 4){
            //收藏
            [self makeUrl_post_postCollection];
        }
        else if (index == 5){
            //推荐
            [self makeUrl_post_postShare];
        }
        else if (index == 6){
            //悄悄关注
            [self makeUrl_post_addFollowFollow_type:@"1"];
        }
        
    }
}
#pragma mark -网络请求
//博见列表
-(void)net_makeUrl_postBJList{
    [[DDNetworkManagerDate makeUrl_postBJ_postListWithtoken:k_user_token post_type:@"2" post_order:post_order user_sex:user_sex user_active:user_active region_id:region_id user_age:user_age user_hobby:user_hobby user_occupation:user_occupation user_type:user_type user_rank:user_rank is_search:is_search post_cate:post_cate page:[NSString stringWithFormat:@"%d",_page]] post_RequestFinshSuccess:^(id responseObject) {
        
//        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            //            _ListmodelArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                BJDynamicSnapshotModel *model = [BJDynamicSnapshotModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            //            self.ListmodelArr = arr.mutableCopy;
            if (_page == 1) {
                self.ListmodelArr = arr.mutableCopy;
            }else{
                [self.ListmodelArr addObjectsFromArray:arr];
            }
            
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//删除博见
-(void)makeUrl_post_delPost{
    [[DDNetworkManagerDate makeUrl_post_delPostWithtoken:k_user_token post_id:[_Exitmodel.post_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//举报博见
-(void)makeUrl_post_addReportrePort_type:(NSString *)report_type{
    [[DDNetworkManagerDate makeUrl_post_addReportWithtoken:k_user_token post_id:[_Exitmodel.post_id stringValue] report_type:report_type] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//拉黑
-(void)makeUrl_post_addBlack{
    [[DDNetworkManagerDate makeUrl_post_addBlackWithtoken:k_user_token user_id:[_Exitmodel.user_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            [self net_makeUrl_postBJList];
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//只给一窗
-(void)makeUrl_post_addOne{
    [[DDNetworkManagerDate makeUrl_post_addOneWithtoken:k_user_token user_id:[_Exitmodel.user_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
        //        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//关注
-(void)makeUrl_post_addFollowFollow_type:(NSString *)follow_type{
    [[DDNetworkManagerDate makeUrl_post_addFollowWithtoken:k_user_token user_id:[_Exitmodel.user_id stringValue] follow_type:follow_type] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//收藏
-(void)makeUrl_post_postCollection{
    [[DDNetworkManagerDate makeUrl_post_postCollectionWithtoken:k_user_token post_id:[_Exitmodel.post_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//推荐
-(void)makeUrl_post_postShare{
    [[DDNetworkManagerDate makeUrl_post_postShareWithtoken:k_user_token post_id:[_Exitmodel.post_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
        }
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}
-(NSMutableArray *)ListmodelArr{
    if (!_ListmodelArr) {
        _ListmodelArr = [NSMutableArray array];
    }
    return _ListmodelArr;
}
- (void)JsonRequest{
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
